package bb.lanxing.devices.sync;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import com.garmin.fit.Mesg;
import com.garmin.fit.SessionMesg;
import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.common.config.Constants;
import bb.lanxing.lib.devices.core.sync.DeviceFile;
import bb.lanxing.lib.devices.core.sync.DeviceSyncListener;
import bb.lanxing.lib.devices.core.sync.FitDeviceFile;
import bb.lanxing.lib.devices.sprint.Command;
import bb.lanxing.lib.devices.sprint.Commands;
import bb.lanxing.lib.devices.sprint.SprintFile;
import bb.lanxing.lib.devices.sprint.SprintListener;
import bb.lanxing.lib.devices.sprint.XossDeviceController;
import bb.lanxing.lib.devices.sprint.utils.SprintFileHelper;
import bb.lanxing.lib.devices.sync.FitProcessor;
import bb.lanxing.lib.devices.sync.FitTrans;
import bb.lanxing.util.AppFileUtils;
import bb.lanxing.util.NotificationConstants;
import bb.lanxing.util.text.TextUtils;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractGenericDeviceSyncManager extends FitSyncManager implements SprintListener {
    private static final SimpleDateFormat FILE_NAME_FORMMATER = new SimpleDateFormat("yyyyMMddHHmmss");
    private static final int NOTIFY_ID = 1000;
    private XossDeviceController controller;
    private long deletingFileId;
    private String rootDir;

    @Override
    public /* bridge */ /* synthetic */ void abort() {
        super.abort();
    }

    @Override
    public /* bridge */ /* synthetic */ void abortAll() {
        super.abortAll();
    }

    @Override
    public /* bridge */ /* synthetic */ void deleteAll() {
        super.deleteAll();
    }

    @Override
    public /* bridge */ /* synthetic */ DeviceFile getCurrentItem() {
        return super.getCurrentItem();
    }

    @Override
    public /* bridge */ /* synthetic */ DeviceFile getItem(long j) {
        return super.getItem(j);
    }

    @Override
    public /* bridge */ /* synthetic */ int getSyncState(long j) {
        return super.getSyncState(j);
    }

    @Override
    public /* bridge */ /* synthetic */ Object getTag() {
        return super.getTag();
    }

    @Override
    public /* bridge */ /* synthetic */ boolean isSynchronised(long j) {
        return super.isSynchronised(j);
    }

    @Override
    public /* bridge */ /* synthetic */ boolean isSynchronising() {
        return super.isSynchronising();
    }

    @Override
    public /* bridge */ /* synthetic */ boolean isSynchronisingWithMultiFiles() {
        return super.isSynchronisingWithMultiFiles();
    }

    @Override
    public /* bridge */ /* synthetic */ void onError(FitTrans fitTrans, Throwable th) {
        super.onError(fitTrans, th);
    }

    @Override
    public /* bridge */ /* synthetic */ void onFitReceived(FitTrans fitTrans) {
        super.onFitReceived(fitTrans);
    }

    @Override
    public /* bridge */ /* synthetic */ void onMesg(FitTrans fitTrans, Mesg mesg) {
        super.onMesg(fitTrans, mesg);
    }

    @Override
    public /* bridge */ /* synthetic */ void onSport(FitTrans fitTrans, SessionMesg sessionMesg) {
        super.onSport(fitTrans, sessionMesg);
    }

    @Override
    public /* bridge */ /* synthetic */ void readFileList() {
        super.readFileList();
    }

    @Override
    public /* bridge */ /* synthetic */ void registerSyncListener(DeviceSyncListener deviceSyncListener) {
        super.registerSyncListener(deviceSyncListener);
    }

    @Override
    public /* bridge */ /* synthetic */ boolean resync(DeviceFile deviceFile) {
        return super.resync(deviceFile);
    }

    @Override
    public /* bridge */ /* synthetic */ void setCurrentFile(DeviceFile deviceFile) {
        super.setCurrentFile(deviceFile);
    }

    @Override
    public /* bridge */ /* synthetic */ void setTag(Object obj) {
        super.setTag(obj);
    }

    @Override
    public /* bridge */ /* synthetic */ boolean shouldSyncWithServer() {
        return super.shouldSyncWithServer();
    }

    @Override
    public /* bridge */ /* synthetic */ boolean sync() {
        return super.sync();
    }

    @Override
    public /* bridge */ /* synthetic */ boolean sync(long j) {
        return super.sync(j);
    }

    @Override
    public /* bridge */ /* synthetic */ boolean sync(DeviceFile deviceFile) {
        return super.sync(deviceFile);
    }

    @Override
    public /* bridge */ /* synthetic */ boolean sync(String str) {
        return super.sync(str);
    }

    @Override
    public /* bridge */ /* synthetic */ void unregisterSyncListener(DeviceSyncListener deviceSyncListener) {
        super.unregisterSyncListener(deviceSyncListener);
    }

    @Override
    public void init(FitProcessor fitProcessor, String str) {
        super.init(fitProcessor, str);
        this.rootDir = str;
    }

    @Override
    protected void showNotificationAfterFinish(int i, int i2) {
        App context = App.getContext();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);//b.m
        //TODO:hu
//        new Intent(context, MainTabActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Notification build = new NotificationCompat
                .Builder(context, NotificationConstants.CHANNEL_ID_SYNC_WORKOUT)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setTicker(context.getString(R.string.str_sync_notification_ticker))
                .setChannelId(NotificationConstants.CHANNEL_ID_SYNC_WORKOUT)
                .setContentTitle(context.getString(R.string.str_sync_notification_message_detail, i, i2))
                .setAutoCancel(true)
                .setStyle(new NotificationCompat
                        .BigTextStyle()
                        .setBigContentTitle(context.getString(R.string.str_sync_notification_ticker))
                        .bigText(context.getString(R.string.str_sync_notification_message_detail, i, i2)))
                .setDefaults(-1)
                .build();
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel notificationChannel =
                    new NotificationChannel(NotificationConstants.CHANNEL_ID_SYNC_WORKOUT, "蓝行骑行", NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setDescription("蓝行骑行同步服务");
            notificationChannel.enableVibration(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }
        notificationManager.notify(1000, build);
    }

    @Override
    void doReadFileList() {
        if (this.controller == null) {
            return;
        }
        File file = new File(this.rootDir, SprintFileHelper.FILE_LIST);
        if (file.exists()) {
            onGetFileList(SprintFileHelper.parseFileList(this.rootDir, file.getAbsolutePath()));
        } else {
            this.controller.getFileList();
        }
    }

    @Override
    boolean doSync(DeviceFile deviceFile) {
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController == null) {
            return false;
        }
        xossDeviceController.getFile(deviceFile.getName());
        return true;
    }

    @Override
    public void onProgressUpdate(Command command, float f) {
        DeviceFile currentItem = getCurrentItem();
        if (currentItem != null) {
            notifyProgressUpdate(currentItem.getId(), f);
        }
    }

    @Override
    public void onCmdStatus(Command command, int i, byte[] bArr) {
        DeviceFile currentItem;
        byte cmd = command.getCmd();
        String parseFileName = Commands.parseFileName(command);
        if (cmd == 5) {
            if (!parseFileName.endsWith(Constants.FIT_DIR) || (currentItem = getCurrentItem()) == null || i == 0) {
                return;
            }
            notifySyncStatus(currentItem.getId(), 4);
            String path = getPath(currentItem.getId());
            if (TextUtils.isEmptyOrNull(path)) {
                return;
            }
            AppFileUtils.deleteFile(path);
        } else if (cmd != 13 || !parseFileName.endsWith(Constants.FIT_DIR)) {
        } else {
            notifySyncStatus(this.deletingFileId, i == 0 ? 6 : 7, (String) null, false);
            removeById(this.deletingFileId);
            this.deletingFileId = -1L;
        }
    }

    @Override
    public void onFileReceived(String str) {
        if (SprintFileHelper.isFileList(str)) {
            onGetFileList(SprintFileHelper.parseFileList(this.rootDir, str));
            return;
        }
        FitTrans fitTrans = FitTrans.get();
        DeviceFile currentItem = getCurrentItem();
        if (currentItem == null) {
            return;
        }
        fitTrans.setId(currentItem.getId());
        fitTrans.setPath(str);
        onFitReceived(fitTrans);
    }

    public void onGetFileList(List<SprintFile> list) {
        if (list != null) {
            List<DeviceFile> arrayList = new ArrayList<>();
            for (SprintFile sprintFile : list) {
                FitDeviceFile fitDeviceFile = new FitDeviceFile();
                fitDeviceFile.id = generateFileId();
                fitDeviceFile.timestamp = fitDeviceFile.id;
                fitDeviceFile.localTimestamp = fitDeviceFile.id;
                fitDeviceFile.size = sprintFile.getSize();
                fitDeviceFile.name = sprintFile.getFileName();
                fitDeviceFile.path = sprintFile.getPath();
                fitDeviceFile.startTime = parseStartTime(sprintFile.getFileName());
                arrayList.add(fitDeviceFile);
            }
            notifyReadFileList(arrayList);
            return;
        }
        notifyReadFileList(null);
    }

    @Override
    public void delete(long j) {
        DeviceFile fileById;
        if (this.controller == null || isSynchronising() || (fileById = getFileById(j)) == null) {
            return;
        }
        this.deletingFileId = fileById.getId();
        this.controller.delete(fileById.getName());
    }

    @Override
    protected boolean exists(long j) {
        return exists(getFileById(j));
    }

    public boolean exists(DeviceFile deviceFile) {
        if (deviceFile != null) {
            File file = new File(deviceFile.getPath());
            return file.exists() && file.length() == deviceFile.getSize();
        }
        return false;
    }

    @Override
    public String getPath(long j) {
        return getItem(j).getPath();
    }

    private long parseStartTime(String str) {
        int lastIndexOf = str.lastIndexOf(".");
        if (lastIndexOf > 0) {
            try {
                return FILE_NAME_FORMMATER.parse(str.substring(0, lastIndexOf)).getTime();
            } catch (ParseException unused) {
                return -1L;
            }
        }
        return -1L;
    }

    public void setSprintController(XossDeviceController xossDeviceController) {
        XossDeviceController xossDeviceController2 = this.controller;
        if (xossDeviceController2 != null) {
            xossDeviceController2.registerXossDeviceListener(this);
        }
        this.controller = xossDeviceController;
        if (xossDeviceController != null) {
            xossDeviceController.registerXossDeviceListener(this);
        }
    }

    @Override
    public void release() {
        super.release();
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController != null) {
            xossDeviceController.unregisterXossDeviceListener(this);
        }
        this.controller = null;
    }
}
